; (function(a) {
	function b(i, g) {
		var h = i.html(),
		f = this,
		d = {
			title: "",
			cls: "",
			autoClose: false,
			multi: false,
			url: "",
			param: {},
			callback: {}
		},
		c = a.extend({},
		d, g);
		this._multi = c.multi;
		this.dialog = dui.Dialog({
			title: c.title,
			cls: c.cls,
			width: 500,
			content: h
		},
		c.multi);
		this.dialog.node.addClass("dialog-tags");
		this.tagEditor = {};
		f._initDialogFrm(c.url, c.param, c.callback);
		if (c.autoClose) {
			a("body").bind("click", e)
		}
		function e(j) {
			if (!a.contains(f.dialog.node[0], j.target)) {
				f.close()
			}
		}
		f.dialog.node.bind("dialog:close",
		function() {
			if (f._multi) {
				f.removeDialog()
			}
			a("body").unbind("click", e)
		})
	}
	b.prototype = {
		_initDialogFrm: function(e, h, j) {
			var g = this.dialog.node.find("form"),
			f = this.dialog.node.find('[type="submit"]'),
			i = this.dialog.node.find(".tag-editor-bd"),
			c = this,
			d = "";
			c.tagEditor = i.TagEditor().get(0);
			c._extendTagEditor(i);
			c.tagEditor.on("plugin:expand error error:hidden",
			function() {
				c.dialog.update()
			});
			if (!i.find(".tags-folder span:gt(5)").length) {
				i.find(".tags_more").hide();
				c.dialog.update()
			}
			f.click(function(k) {
				k.preventDefault();
				d = c.tagEditor.plainString();
				if (a.isFunction(j)) {
					j.call(c, d)
				} else {
					if (e) {
						f.attr("disabled", "disabled").addClass("bn-disable").val("提交中…");
						a.post_withck(e, a.extend({
							author_tags: d
						},
						h),
						function() {
							f.removeAttr("disabled");
							c.dialog.close();
							document.location.reload()
						},
						"POST")
					}
				}
			})
		},
		_extendTagEditor: function(i) {
			var d = a(".tags-tip", i),
			h = a(".tags-folder span", i),
			g = a(".tags-control", i),
			c = a(".tags-folder", i),
			f = this.tagEditor,
			e = this;
			f.on("error",
			function(k, j) {
				switch (j.type) {
				case "exist":
					var l = f.taglist[j.msg].node;
					l.addClass("tags-warn");
					setTimeout(function() {
						l.removeClass("tags-warn")
					},
					1000);
					f.dom.tag_input.val("");
					break;
				default:
					!d.is(":visible") && d.text(j.msg).fadeIn().delay(2000).fadeOut(function() {
						f.fire("error:hidden")
					});
					break
				}
			}).on("change",
			function(k) {
				var j = f.dom.tag_writer.find("label");
				h.each(function(m, l) {
					l = a(l);
					f.indexOf(l.text()) > -1 ? l.addClass("tag-selected") : l.removeClass("tag-selected")
				});
				f.taglist.length ? j.addClass("invisible") : j.removeClass("invisible")
			});
			g.delegate(".tags_more", "click",
			function(j) {
				j.preventDefault();
				c.addClass("tags-unfold");
				f.fire("plugin:expand");
				a(this).hide()
			});
			c.delegate("span", "click",
			function() {
				var j = a(this);
				if (j.hasClass("tag-selected")) {
					f.remove(j.text())
				} else {
					f.insert(j.text())
				}
			});
			f.dom.tag_writer.find("label").removeClass("invisible")
		},
		show: function(h, f, e, j, i) {
			var d = this,
			c = [];
			if (f) {
				d.tagEditor.removeAllTags();
				d.tagEditor.renderFromString(f)
			}
			d.dialog.open();
			if (h) {
				d.dialog.setTitle(h).update()
			}
			if (e && j) {
				var g = d.dialog.node;
				if (i && i === "right") {
					c[0] = j[0] - g.width() + parseInt(g.find(".dui-dialog-shd").css("left"), 10) + j[2]
				}
				if (!c[0] || c[0] < 0) {
					c[0] = j[0] - parseInt(g.find(".dui-dialog-shd").css("left"), 10)
				}
				c[1] = j[1] - g.height() - 22;
				d.dialog.set({
					isStick: e,
					position: c
				})
			}
			return this
		},
		close: function() {
			this.dialog.close();
			if (this._multi) {
				this.removeDialog()
			}
			return this
		},
		removeDialog: function() {
			this.dialog.node.remove();
			return this
		}
	};
	a.fn.TagEditorDialog = function(d, c) {
		return new b(d, c)
	}
})(jQuery);